﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.Category;

public partial class admin_webparts_Plug_ins_Category_ItemInput : System.Web.UI.UserControl
{
    public Category _Cat = null;
    public CatItem _Item = null;
    public string _ValueValidatorFunction = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave.Value = btnSave2.Value = LanguageUtility.GetLanguageText("SAVE", Session);
        chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        chkDefaultSelect.Text = LanguageUtility.GetLanguageText("DEFAULT_SELECT", Session);

        int langID = LanguageUtility.GetLanguageID();

        try
        {
            int catID = Convert.ToInt32(Request.QueryString["catid"]);
            _Cat = new Category(catID);
            if (_Cat.ID < 1)
            {
                throw new Exception();
            }
            divValueNote.InnerHtml = langID == 1 ? "Giá trị là " : "Value is ";
            switch (_Cat.ValueType)
            {
                case NTTHAO.Web.BL.Category.ValueType.Text:
                    divValueNote.InnerHtml += "text";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.Integer:
                    divValueNote.InnerHtml += langID == 1 ? "số nguyên" : "an integer";
                    _ValueValidatorFunction = "NTTHAO.validator.isInteger";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.Decimal:
                    divValueNote.InnerHtml += langID == 1 ? "số thập phân" : "a decimal";
                    _ValueValidatorFunction = "NTTHAO.validator.isDecimal";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.NaturalNumber:
                    divValueNote.InnerHtml += langID == 1 ? "số tự nhiên (>=0)" : "a natural number (>=0)";
                    _ValueValidatorFunction = "NTTHAO.validator.isNaturalNumber";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.PositiveInteger:
                    divValueNote.InnerHtml += langID == 1 ? "số nguyên dương (>0)" : "a positive integer (>=0)";
                    _ValueValidatorFunction = "NTTHAO.validator.isPositiveInteger";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.PositiveDecimal:
                    divValueNote.InnerHtml += langID == 1 ? "số thập phân dương (>0)" : "a positive decimal (>=0)";
                    _ValueValidatorFunction = "NTTHAO.validator.isPositiveDecimal";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.NegativeInteger:
                    divValueNote.InnerHtml += langID == 1 ? "số nguyên âm (<0)" : "a negative integer (<0)";
                    _ValueValidatorFunction = "NTTHAO.validator.isNegativeInteger";
                    break;
                case NTTHAO.Web.BL.Category.ValueType.NegativeDecimal:
                    divValueNote.InnerHtml += langID == 1 ? "số thập phân âm (<0)" : "a negative decimal (<0)";
                    _ValueValidatorFunction = "NTTHAO.validator.isNegativeDecimal";
                    break;
                default:
                    break;
            }

            if (!IsPostBack) 
            {
                if ("edititem".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
                {
                    int itemID = Convert.ToInt32(Request.QueryString["id"].Trim());
                    _Item = new CatItem(itemID);
                    if (_Item.ID < 1)
                    {
                        throw new Exception();
                    }
                    txtName.Text = _Item.Name;
                    txtValue.Text = _Item.Value;
                    chkDefaultSelect.Checked = _Item.DefaultSelect;
                    chkActive.Checked = _Item.Actived;
                }
            }
        }
        catch 
        {
            string redirectURL = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"];
            if (Request.QueryString["catid"] != null)
            {
                redirectURL += "&action=viewcat&id=" + Request.QueryString["catid"];
            }
            Response.Redirect(redirectURL, false);
            return;
        }
    }

    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CatItem item = null;
            if ("edititem".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                int itemID = Convert.ToInt32(Request.QueryString["id"].Trim());
                item = new CatItem(itemID);
            }
            else
            {
                item = new CatItem();
                item.CatID = _Cat.ID;
            }

            item.Name = txtName.Text.Trim();
            item.Value = txtValue.Text.Trim();
            item.DefaultSelect = chkDefaultSelect.Checked;
            item.Actived = chkActive.Checked;

            if (_Cat.SelectType == SelectType.SingleSelect && item.DefaultSelect == true)
            {
                _Cat.ClearDefaultSelect();
            }

            if ("edititem".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                item.Update();
            }
            else
            {
                item.Insert();
            }
        }
        catch { }

        string redirectURL = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"];
        if (Request.QueryString["catid"] != null)
        {
            redirectURL += "&action=viewcat&id=" + Request.QueryString["catid"];
        }
        Response.Redirect(redirectURL, false);
        return;
    }
}
